package com.it.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.it.reggie.Exception.BusinessException;
import com.it.reggie.commen.R;
import com.it.reggie.entity.AddressBook;
import com.it.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/addressBook")
@SuppressWarnings(value = "all")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    //添加地址
    @PostMapping
    public R add(@RequestBody AddressBook addressBook, HttpServletRequest request) {

        Long userID = (Long) request.getSession().getAttribute("user");

        System.out.println("======");
        System.out.println(userID);
        addressBook.setUserId(userID);

        addressBookService.save(addressBook);

        return R.success("保存地址成功");

    }

    //修改为默认地址
    @PutMapping("/default")
    public R defaultAddress(@RequestBody AddressBook addressBook,HttpServletRequest request) {

        Long userId = (Long) request.getSession().getAttribute("user");

        Long id = addressBook.getId();

        AddressBook addressBook1 = new AddressBook();

        addressBook1.setIsDefault(0);

        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(AddressBook::getUserId, userId);

        addressBookService.update(addressBook1, queryWrapper);

        addressBook.setIsDefault(1);

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.eq(AddressBook::getId, id);

        addressBookService.update(addressBook, qw);

        return R.success("设置成功");

    }

    //查询默认地址
    @GetMapping("/default")
    public R selectDefaultInfo(HttpServletRequest request){

        Long userId = (Long) request.getSession().getAttribute("user");

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.eq(AddressBook::getUserId , userId).eq(AddressBook::getIsDefault, 1);

        AddressBook addressBookServiceOne = addressBookService.getOne(qw);

        if (addressBookServiceOne!=null) {

            return R.success(addressBookServiceOne);
        }else {

            throw new BusinessException("没有设置默认地址");
        }

    }

    //根据用户id查询所有地址信息
    @GetMapping("/list")
    public R selectAll(HttpServletRequest request) {

        Long userID = (Long) request.getSession().getAttribute("user");

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.eq(AddressBook::getUserId, userID);

        List<AddressBook> list = addressBookService.list(qw);

        return R.success(list);

    }

    //根据id查询数据，数据回显
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.eq(AddressBook::getId, id);

        AddressBook addressBookServiceOne = addressBookService.getOne(qw);

        return R.success(addressBookServiceOne);
    }

    //删除地址
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids) {

        addressBookService.removeByIds(ids);

        return R.success("删除成功");
    }

    //修改地址信息
    @PutMapping
    public R selectAddress(@RequestBody AddressBook addressBook){

        Long id = addressBook.getId();

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();

        qw.eq(AddressBook::getId, id);

        addressBookService.update(addressBook, qw);

        return R.success("修改成功");

    }


}
